package com.runen.infinitemovement.alioss;

import android.content.Context;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSAuthCredentialsProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;


/**
 * Create by LiaoDuanHong
 * Create on 2020/06/17 09:33
 */
public class AliInitUtils {
    private String endpoint;
    private String stsServer;

    public AliInitUtils(String endpoint, String stsServer) {
        this.endpoint = endpoint;
        this.stsServer = stsServer;
    }

    public OSS initOSS(Context context) {
// 推荐使用OSSAuthCredentialsProvider。token过期可以及时更新。
//        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(ACCESS_ID, ACCESS_KEY);
        OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);

// 配置类如果不设置，会有默认配置。
        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒。
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒。
        conf.setMaxConcurrentRequest(5); // 最大并发请求数，默认5个。
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次。
        OSSLog.enableLog();
        OSS oss = new OSSClient(context, endpoint, credentialProvider, conf);
        return oss;
    }
}
